//
//  ZYStatusCell.swift
//  CZ29WeiboZYH
//
//  Created by 沐颜澈 on 16/6/27.
//  Copyright © 2016年 muyanche. All rights reserved.
//

import UIKit
import SnapKit

//状态边距
let StatusMargin: CGFloat = 10


class ZYStatusCell: UITableViewCell {
    
    //声明一个全局的约束
    var toolBarTopConstraint:Constraint?
    
    
    //629=====,是可选的值
    var status:ZYStatusModel? {
        didSet {
            //给原创微博赋值
            statusOriginalView.status = status
            
            //赋值629=======================
           // let s = status?.retweeted_status?.text
            //print(s)
            
            /*
            判断是否有转发微博
            如果retweeted_status == nil没有转发微博
            toolbar的top == 原创微博的底部
            隐藏 转发微博(注意cell的重用问题)
            - 如果retweeted_status != nil 有转发微博
            //有转发微博就必须给赋值
            - 给其转发微博view赋值
            - toolbar 的top == 转发微博的底部
            - 显示 转发微博(注意cell重用问题)
            
            */
            
            //先卸载原有的约束
            toolBarTopConstraint?.uninstall()
            
            //没有转发微博
            if status?.retweeted_status == nil {
//                 隐藏 转发微博(注意cell的重用问题)
                statusRetweetView.hidden = true
                //使用make其实和使用Update时一样的
                //如果已经下载约束了使用make和update其实是一样的
                statusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    //记录一下
                   toolBarTopConstraint = make.top.equalTo(statusOriginalView.snp_bottom).constraint
                    
                })
            
            } else {
                
                //有转发微博进行赋值
                statusRetweetView.status = status
                
                statusRetweetView.hidden = false
                statusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                   toolBarTopConstraint =  make.top.equalTo(statusRetweetView.snp_bottom).constraint
                    
                })
            
            }
            
            statusToolBar.status = status
            
            
        }
        
    }
        
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        
    }

   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
    
    
    //设置UI
    private func setupUI() {
        
        contentView.backgroundColor = UIColor.whiteColor()
        
        //添加控件
        contentView.addSubview(statusOriginalView)
        contentView.addSubview(statusToolBar)
        contentView.addSubview(statusRetweetView)
        
        
        //添加约束
        statusOriginalView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            //629.由于约束了原创的微博的高度是动态的随着微博的内容的高度来决定的所以这里就不能把
            //微博cell的高度个写死了
//            make.height.equalTo(50)/
            //7.1 设置top
            make.top.equalTo(contentView).offset(10)
        }
        
        //转发微博的约束
        statusRetweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(statusOriginalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
           // make.height.equalTo(50)
            
        }
        
//        添加评论赞和转发的约束
        statusToolBar.snp_makeConstraints { (make) -> Void in
            
            //少写了右侧,会看不见
            make.leading.trailing.equalTo(contentView)
            
            /*629 10:00 加上constraint
            记录约束
            */
            
            toolBarTopConstraint = make.top.equalTo(statusRetweetView.snp_bottom).constraint
            
            make.height.equalTo(35)
            
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(statusToolBar)
        }
        

    }
    
    //MARK: - 懒加载控件
    private lazy var statusOriginalView: ZYStatusOriginalView = ZYStatusOriginalView()
    
    //
    //底部工具条的评论转发和赞
    private lazy var statusToolBar:ZYStatusToolBar = ZYStatusToolBar()
    
    //转发微博
    private lazy var statusRetweetView:ZYStatusRetweetView = ZYStatusRetweetView()
    
    
    
}




extension ZYStatusModel {

    func demo() {
        
//        /(
//            "<SnapKit.LayoutConstraint:0x7fbed41c3050@/Users/muyanche/Code/Swift/03/CZ29WeiboZYH/CZ29WeiboZYH/CZ29WeiboZYH/CZ29WeiboZYH/Classes/Home/Views/ZYStatusRetweetView.swift#39 UILabel:0x7fbed41c2610.top == CZ29WeiboZYH.ZYStatusRetweetView:0x7fbed41c2490.top + 10.0>",
//            "<SnapKit.LayoutConstraint:0x7fbed41c2b20@/Users/muyanche/Code/Swift/03/CZ29WeiboZYH/CZ29WeiboZYH/CZ29WeiboZYH/CZ29WeiboZYH/Classes/Home/Views/ZYStatusRetweetView.swift#45 CZ29WeiboZYH.ZYStatusRetweetView:0x7fbed41c2490.bottom == UILabel:0x7fbed41c2610.bottom + 10.0>",
//            "<SnapKit.LayoutConstraint:0x7fbed41c7ce0@/Users/muyanche/Code/Swift/03/CZ29WeiboZYH/CZ29WeiboZYH/CZ29WeiboZYH/CZ29WeiboZYH/Classes/Home/Views/ZYStatusCell.swift#111 CZ29WeiboZYH.ZYStatusRetweetView:0x7fbed41c2490.top == CZ29WeiboZYH.ZYStatusOriginalView:0x7fbed41b4130.bottom>",
//            "<SnapKit.LayoutConstraint:0x7fbed41c9e40@/Users/muyanche/Code/Swift/03/CZ29WeiboZYH/CZ29WeiboZYH/CZ29WeiboZYH/CZ29WeiboZYH/Classes/Home/Views/ZYStatusCell.swift#119 CZ29WeiboZYH.ZYStatusToolBar:0x7fbed41ba570.top == CZ29WeiboZYH.ZYStatusRetweetView:0x7fbed41c2490.bottom>",
//            "<SnapKit.LayoutConstraint:0x7fbed41d0ad0@/Users/muyanche/Code/Swift/03/CZ29WeiboZYH/CZ29WeiboZYH/CZ29WeiboZYH/CZ29WeiboZYH/Classes/Home/Views/ZYStatusCell.swift#54 CZ29WeiboZYH.ZYStatusToolBar:0x7fbed41ba570.top == CZ29WeiboZYH.ZYStatusOriginalView:0x7fbed41b4130.bottom>"
//        )
//        
//        Will attempt to recover by breaking constraint
//        <SnapKit.LayoutConstraint:0x7fbed41c2b20@/Users/muyanche/Code/Swift/03/CZ29WeiboZYH/CZ29WeiboZYH/CZ29WeiboZYH/CZ29WeiboZYH/Classes/Home/Views/ZYStatusRetweetView.swift#45 CZ29WeiboZYH.ZYStatusRetweetView:0x7fbed41c2490.bottom == UILabel:0x7fbed41c2610.bottom + 10.0>
//        

        //        )

    
    
    }

}























